package com.ruoyi.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.operation.domain.bo.TItdutytaskBo;
import com.ruoyi.operation.domain.vo.TItdutytaskVo;
import com.ruoyi.operation.domain.TItdutytask;
import com.ruoyi.operation.mapper.TItdutytaskMapper;
import com.ruoyi.operation.service.ITItdutytaskService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * it值班检查任务任务Service业务层处理
 *
 * @author chq
 * @date 2025-07-04
 */
@RequiredArgsConstructor
@Service
public class TItdutytaskServiceImpl implements ITItdutytaskService {

    private final TItdutytaskMapper baseMapper;

    /**
     * 查询it值班检查任务任务
     */
    @Override
    public TItdutytaskVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询it值班检查任务任务列表
     */
    @Override
    public TableDataInfo<TItdutytaskVo> queryPageList(TItdutytaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TItdutytask> lqw = buildQueryWrapper(bo);
        Page<TItdutytaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询it值班检查任务任务列表
     */
    @Override
    public List<TItdutytaskVo> queryList(TItdutytaskBo bo) {
        LambdaQueryWrapper<TItdutytask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TItdutytask> buildQueryWrapper(TItdutytaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TItdutytask> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBusisystemid()), TItdutytask::getBusisystemid, bo.getBusisystemid());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TItdutytask::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getTaskname()), TItdutytask::getTaskname, bo.getTaskname());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskstep()), TItdutytask::getTaskstep, bo.getTaskstep());
        lqw.eq(StringUtils.isNotBlank(bo.getPeriod()), TItdutytask::getPeriod, bo.getPeriod());
        lqw.eq(bo.getStarttime() != null, TItdutytask::getStarttime, bo.getStarttime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsauto()), TItdutytask::getIsauto, bo.getIsauto());
        lqw.eq(StringUtils.isNotBlank(bo.getMemo()), TItdutytask::getMemo, bo.getMemo());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), TItdutytask::getFlag, bo.getFlag());
        return lqw;
    }

    /**
     * 新增it值班检查任务任务
     */
    @Override
    public Boolean insertByBo(TItdutytaskBo bo) {
        TItdutytask add = BeanUtil.toBean(bo, TItdutytask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改it值班检查任务任务
     */
    @Override
    public Boolean updateByBo(TItdutytaskBo bo) {
        TItdutytask update = BeanUtil.toBean(bo, TItdutytask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TItdutytask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除it值班检查任务任务
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
